package isstra.gestore;
import sde.semaforo.*;
public class GestoreS implements Gestore {
	private boolean release=false;
	private int cnt=0, w=0;
	private SemaforoBinario mutex=new SemaforoBinario( 1 );
	private SemaforoBinario attesa=new SemaforoBinario( 0 );
	public void request(){
		mutex.P();
		if( release ){ w++; mutex.V(); attesa.P(); w--; }
		cnt++;
		if( cnt==3 ) release=true;
		System.out.println(Thread.currentThread().getName()+" entra");
		if( !release && w>0 ) attesa.V();
		else mutex.V();
	}//request
	public void finish(){
		mutex.P();
		cnt--;
		System.out.println(Thread.currentThread().getName()+" esce");
		if( cnt==0 ){
			release=false;
			if( w>0 ) attesa.V();
			else mutex.V();
		}
		else mutex.V();
	}//finish
}//GestoreS
